<template>
  <div class="app-container flex-col minH">
    <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
      <div class="align-center flex-wrap">
        <el-input v-model="id" style="width: 280px" placeholder="搜尋ID..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="name" style="width: 280px" placeholder="搜尋昵稱..." :prefix-icon="Search" class="search mr10 mb10" />
        <el-input v-model="phone" style="width: 280px" placeholder="搜尋賬號..." :prefix-icon="Search" class="search mr10 mb10" />
        <div class="align-center mb10">
          <el-date-picker v-model="start1" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
          <span class="plr10 clr9fa">--</span>
          <el-date-picker v-model="end1" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
        </div>

      </div>
      <div class="noshrink">
        <el-button class="btn2" @click="search(1)">查詢</el-button>
        <el-button class="btn1" @click="filterReset(1)">重置</el-button>
      </div>
    </div>
    <div class="bgblueg ptb20 radius10 w100p mt15 pl10 bold clr3c4">
      <span>總充值金額：{{ charge }}元</span>
      <span class="pl80">總消費金額：{{ consumption }}元</span>

    </div>
    <div class="mt15 radius10 hidden pb15 flex1 bgwhite  flex-col w100p">
      <div class="flex1 w100p ">
        <el-table :data="userTableData" style="width: 100%" cell-class-name="tbcell" header-cell-class-name="hdcell">
          <el-table-column prop="id" label="用戶ID" width="100">
            <template #default="scope">
              <span @click="showUser(scope.row.id)">ID:{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="phone" label="賬號" width="150" />
          <el-table-column prop="name" label="昵稱" width="150" />
          <el-table-column prop="avatar" label="頭像" width="100">
            <template #default="scope">
              <!-- {{ scope }} -->
              <el-avatar shape="square" :size="50" :fit="'cover'" :src="scope.row.avatar" />
            </template>
          </el-table-column>
          <el-table-column prop="coin" label="餘額" width="150" />
          <el-table-column prop="invite_num" label="邀請人數" width="100">
            <template #default="scope">
              <div class="align-center" @click="dialogShow(3, scope.row)">
                <span class="mr5">{{ scope.row.invite_num }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="is_broker_text" label="是否是經紀人" width="150" />

          <el-table-column prop="is_certification" label="實名認證" width="100">
            <template #default="scope">
              <div class="blue" v-if="scope.row.is_certification">已認證</div>
              <div class="red" v-else>未認證</div>
            </template>
          </el-table-column>
          <el-table-column prop="all_charge" label="縂充值" width="150" sortable>
            <template #default="scope">
              <div class="align-center" @click="dialogShow(4, scope.row)">
                <span class="mr5">{{ scope.row.all_charge }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="consumption" label="縂消費" width="150" sortable>
            <template #default="scope">
              <div class="align-center" @click="dialogShow(5, scope.row)">
                <span class="mr5">{{ scope.row.consumption }}</span>
                <el-icon>
                  <View />
                </el-icon>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="level" label="星級" width="100" />
          <el-table-column prop="vip" label="是否是Vip" width="150" />

          <el-table-column prop="created_at" label="創建時間" width="200" />
          <el-table-column prop="updated_at" label="更新時間" width="200" />
          <el-table-column prop="" label="操作" width="240" fixed="right">
            <template #default="scope">
              <div class="align-center">
                <div class=" pointer" @click="dialogShow(1, scope.row)">餘額變更</div>
                <span class="plr5">|</span>
                <div class=" pointer" @click="edit(scope.row)">編輯</div>
                <span class="plr5">|</span>
                <div class=" pointer red" @click="dialogShow(2, scope.row)">刪除</div>


              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="plr20 mlauto mt20">
        <el-pagination v-model:current-page="currentPage1" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total1" @size-change="handleSizeChange" @current-change="handleCurrentChange1" />
      </div>

    </div>
    <el-dialog v-model="dialogVisible" :width="dialogType == 1 || dialogType == 2 ? '700' : '1000'" :title="dialogType == 1 ? '賬戶餘額變更' : dialogType == 2 ? '刪除' : dialogType == 3 ? '邀請人數詳情' : dialogType == 4 ? '縂充值詳情' : '總消費詳情'">
      <!-- 賬戶餘額變更 -->
      <template v-if="dialogType == 1">
        <div class="mt40 w100p flex-col">
          <el-form :label-position="labelPosition" label-width="auto" :model="formLabelAlign" style="max-width: 600px">
            <el-form-item label="數量:">
              <el-input v-model="formLabelAlign.number" style="width: 380px;box-shadow: none;" placeholder="請輸入修改數量" />
            </el-form-item>
            <el-form-item label="類型:" required>
              <el-radio-group v-model="formLabelAlign.radio" class="ml-4">
                <el-radio value="1" size="large">增加</el-radio>
                <el-radio value="2" size="large">減少</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>
          <div class="mt20 mb15 flex-cen ">
            <div class="align-center">
              <el-button class="btn1" @click="reset('balance')">重置</el-button>
              <el-button class="btn2" @click="alterBalance()">提交</el-button>
            </div>

          </div>
        </div>
      </template>
      <!-- 刪除 -->
      <template v-if="dialogType == 2">
        <div class="flex-col mt20 pb20">
          <div class="flex-col">
            <div class="clr3c4 ft24">提示</div>
            <div class="mt20 clr3c4">確定刪除當前帳號嗎？</div>
            <el-button class="btn2 mt20" @click="delUser()">確定</el-button>
            <div class="mt20 clr9fa" @click="dialogClose">取消</div>
          </div>
        </div>
      </template>
      <!-- 邀請人數詳情 -->
      <template v-if="dialogType == 3">
        <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
          <div class="noshrink">
            <el-date-picker v-model="start2" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
            <span class="plr10">-</span>
            <el-date-picker v-model="end2" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
            <!-- <el-select v-model="value" placeholder="時間篩選" size="large" style="width: 240px">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->

          </div>
          <div class="noshrink">
            <el-button class="btn2" @click="search(2)">查詢</el-button>
            <el-button class="btn1" @click="filterReset(2)">重置</el-button>
          </div>
        </div>

        <div class="">
          <el-table :data="inviteTable" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="" label="用戶訊息">
              <template #default="scope">
                <span>{{ scope.row.phone }}(ID:{{ scope.row.id }})</span>
              </template>
            </el-table-column>
            <el-table-column property="name" label="用戶昵稱" />
            <el-table-column property="avatar" label="用戶頭像">
              <template #default="scope">
                <el-avatar shape="square" :size="50" :fit="'cover'" :src="scope.row.avatar" />
              </template>

            </el-table-column>
            <el-table-column property="created_at" label="邀請時間" />

          </el-table>
          <div class="  plr20 justify-between mt20">
            <div style="flex:1;" class="clr3c4 bold">總共{{ total2 }}條</div>
            <el-pagination v-model:current-page="currentPage2" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total2" @size-change="handleSizeChange" @current-change="handleCurrentChange2" />
          </div>
        </div>

      </template>

      <!-- 縂充值 -->
      <template v-if="dialogType == 4">
        <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
          <div class="noshrink">
            <el-date-picker v-model="start3" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
            <span class="plr10">-</span>
            <el-date-picker v-model="end3" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
            <!-- <el-select v-model="value" placeholder="時間篩選" size="large" style="width: 240px">
              <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
            </el-select> -->

          </div>
          <div class="noshrink">
            <el-button class="btn2" @click="search(3)">查詢</el-button>
            <el-button class="btn1" @click="filterReset(3)">重置</el-button>
          </div>
        </div>

        <div class="">
          <el-table :data="rechargeTable" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="id" label="序號" width="100" />
            <el-table-column property="money" label="充值金額" />
            <el-table-column property="created_at" label="創建時間" width="200" />

          </el-table>
          <div class=" plr20 justify-between mt20">
            <div class="clr3c4 bold noshrink">總共{{ total3 }}條</div>
            <div>
              <div>小計(元): <span class="ft46 clr3c4 ml10">{{ formLabelAlign.totalRecharge }}</span> </div>
            </div>
          </div>
          <div class="flex-col w100p">
            <div class="plr20 mt20 mlauto">
              <el-pagination v-model:current-page="currentPage3" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total3" @size-change="handleSizeChange" @current-change="handleCurrentChange3" />
            </div>
          </div>
        </div>

      </template>
      <!-- 縂消費 -->
      <template v-if="dialogType == 5">
        <div class="topline w100p bgwhite ptb20 radius10 pl10 pr20 justify-between">
          <div class="noshrink">
            <el-date-picker v-model="start4" type="datetime" placeholder="开始时间" value-format="YYYY-MM-DD HH:mm:ss" />
            <span class="plr10">-</span>
            <el-date-picker v-model="end4" type="datetime" placeholder="结束时间" value-format="YYYY-MM-DD HH:mm:ss" />
          </div>
          <div class="noshrink">
            <el-button class="btn2" @click="search(4)">查詢</el-button>
            <el-button class="btn1" @click="filterReset(4)">重置</el-button>
          </div>
        </div>

        <div class="">
          <el-table :data="payTable" cell-class-name="dialogtbcell" header-cell-class-name="dialoghdcell">
            <el-table-column property="id" label="序號" width="100" />
            <el-table-column property="touid" label="消費對象" width="250">
              <template #default="scope">
                <span>ID:{{ scope.row.touid }}</span>
              </template>
            </el-table-column>
            <el-table-column property="type_text" label="消費類型" width="250" />
            <el-table-column property="totalcoin" label="消費金額" />

            <el-table-column property="created_at" label="創建時間" width="200" />

          </el-table>
          <div class=" plr20 justify-between mt20">
            <div class="clr3c4 bold noshrink">總共{{ total4 }}條</div>
            <div>
              <div>小計(元): <span class="ft46 clr3c4 ml10">{{ formLabelAlign.consumption }}</span> </div>
            </div>
          </div>
          <div class="flex-col w100p">
            <div class="plr20 mt20 mlauto">
              <el-pagination v-model:current-page="currentPage4" prev-text="上一頁" next-text="下一頁" :default-page-size="15" :small="small" :disabled="disabled" :background="true" layout="prev, pager, next, jumper" :total="total4" @size-change="handleSizeChange" @current-change="handleCurrentChange4" />
            </div>
          </div>
        </div>

      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { Search } from '@element-plus/icons-vue'
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router';
import { changeCoin, userList, chargeList, deleteUser, inviteList, payList, total } from '@/api/usersManage'
import { inject } from 'vue';
const showUser = inject('showUser');
onMounted(() => {
  getUserList()
  getTotal()

})

const charge = ref('')
const consumption = ref('')
const getTotal = () => {
  total().then((res) => {
    console.log('縂數據', res)
    charge.value = res.data.charge
    consumption.value = res.data.consumption
  })
}
const id = ref('')
const name = ref('')
const phone = ref('')

const start1 = ref('')
const end1 = ref('')
const start2 = ref('')
const end2 = ref('')
const start3 = ref('')
const end3 = ref('')
const start4 = ref('')
const end4 = ref('')
const dialogType = ref(1)

const dialogVisible = ref(false)

const dialogClose = () => {
  dialogVisible.value = false
}
const dialogShow = (type, row) => {
  formLabelAlign.currentId = row.id
  console.log('dialog----show----')
  dialogType.value = type
  dialogVisible.value = true
  // 总充值
  if (type == 4) {
    currentPage3.value = 1
    // formLabelAlign.totalRecharge = row.all_charge
    getRechargeList()
  }
  // 邀请
  if (type == 3) {
    currentPage2.value = 1
    formLabelAlign.invite_num = row.invite_num
    getInviteList()
  }
  // 总消费
  if (type == 5) {
    currentPage4.value = 1
    formLabelAlign.consumption = row.consumption
    getPayList()
  }
}
const delUser = () => {
  console.log('delete---user-----')
  deleteUser({
    id: formLabelAlign.currentId
  }).then((res) => {
    console.log('删除用户', res)
    getUserList()
    dialogClose()

  })
}
// 重置
const reset = (type) => {
  if (type == 'balance') {
    formLabelAlign.radio = '1'
    formLabelAlign.number = ''
  }
}
// 账户余额变更，提交
const alterBalance = () => {
  changeCoin({
    id: formLabelAlign.currentId,
    coin: formLabelAlign.number,
    income_type: formLabelAlign.radio
  }).then((res) => {
    console.log('余额变更成功', res)
    getUserList()
    formLabelAlign.number = ''
    dialogVisible.value = false

  })
}
const formLabelAlign = reactive({
  name: '1',
  region: '',
  type: '',
  number: '',//余额变更数量
  radio: '1',//账户余额变更增加或减少
  currentId: 0,//当前操作用户ID
  totalRecharge: 0,
  invite_num: 0,
  consumption: 0
})
const options = [
  {
    value: 'Option1',
    label: 'Option1',
  },
  {
    value: 'Option2',
    label: 'Option2',
  },
  {
    value: 'Option3',
    label: 'Option3',
  },
  {
    value: 'Option4',
    label: 'Option4',
  },
  {
    value: 'Option5',
    label: 'Option5',
  },
]

const userTableData = ref([])//页面表格
const rechargeTable = ref([])//总充值表格
const inviteTable = ref([])//邀请表格
const payTable = ref([])
const currentPage1 = ref(1)//页面底部分页
const total1 = ref(0)
const currentPage2 = ref(1)//邀请人数列表分页
const total2 = ref(0)
const currentPage3 = ref(1)//总充值列表分页
const total3 = ref(0)
const currentPage4 = ref(1)//总消费列表分页
const total4 = ref(0)
const handleCurrentChange1 = (val) => {
  console.log(`current page: ${val}`)
  getUserList()
}
const handleCurrentChange2 = (val) => {
  getInviteList()
  console.log(`current page: ${val}`)
}
const handleCurrentChange3 = (val) => {
  getRechargeList()
  console.log(`current page: ${val}`)
}
const handleCurrentChange4 = (val) => {
  getPayList()
  console.log(`current page: ${val}`)
}
const router = useRouter()
const toAdd = () => {
  console.log(router)
  router.push({ path: 'addNotice' })
}
const edit = (row) => {
  router.push({ path: 'editVip', query: row })
}
// 重置
const filterReset = (type) => {
  if (type == 1) {
    id.value = ''
    name.value = ''
    phone.value = ''
    start1.value = ''
    end1.value = ''
    getUserList()
  }
  if (type == 2) {
    start2.value = ''
    end2.value = ''
    getInviteList()
  }
  if (type == 3) {
    start3.value = ''
    end3.value = ''
    getRechargeList()
  }
  if (type == 4) {
    start4.value = ''
    end4.value = ''
    getPayList()
  }
}
// 搜尋
const search = (type) => {
  if (type == 1) {

    getUserList()
  }
  if (type == 2) {
    console.log(start.value, end.value)
    getInviteList()
  }
  if (type == 3) {
    getRechargeList()
  }
  if (type == 4) {
    getPayList()
  }

}
// 获取用户列表
const getUserList = () => {
  userList({
    page: currentPage1.value,
    id: id.value,
    name: name.value,
    phone: phone.value,
    start: start1.value,
    end: end1.value
  }).then((res) => {
    console.log('用户列表', res)
    userTableData.value = res.data.data
    total1.value = res.data.total
  })
}
// 获取总充值列表
const getRechargeList = () => {
  chargeList({
    page: currentPage3.value,
    id: formLabelAlign.currentId,
    start: start3.value,
    end: end3.value
  }).then((res) => {
    console.log('总充值列表', res)
    total3.value = res.data.total
    rechargeTable.value = res.data.data
    formLabelAlign.totalRecharge = res.total

  })
}
// 获取邀请人数列表
const getInviteList = () => {
  inviteList({
    id: formLabelAlign.currentId,
    page: currentPage2.value,
    start: start2.value,
    end: end2.value
  }).then((res) => {
    console.log('邀请列表---', res)
    total2.value = res.data.total
    inviteTable.value = res.data.data
  })
}
// 总消费
const getPayList = () => {
  payList({
    id: formLabelAlign.currentId,
    page: currentPage4.value,
    start: start4.value,
    end: end4.value
  }).then((res) => {
    console.log('消费列表', res)
    total4.value = res.data.total
    payTable.value = res.data.data
  })
}

</script>

<style lang="scss" scoped>
.search {
  overflow: hidden;
  border-radius: 10px !important;
  // padding: 5px 0px;
}

:deep(.el-input__wrapper) {
  box-shadow: none;
  // border: none;
}
</style>
<style></style>
 